//
//  AdbTools.m
//  QTools
//
//  Created by zhangqianchu on 2018/11/30.
//  Copyright © 2018年 zhangqianchu. All rights reserved.
//

#import <Foundation/Foundation.h>

#include <stdio.h>
#include <string.h>
//#define ADB_ROOT "/Users/zhangqianchu/Library/Android/sdk/platform-tools/adb "
#define ADB_ROOT "./adb "
#define ADB(params) ADB_ROOT#params

//char* adb_root = "/Users/zhangqianchu/Library/Android/sdk/platform-tools/adb ";
//static char* adb_root = "./adb";

void mysystem(const char* str)
{
    printf("%s\n", str);
    system(str);
}

void _screencap(const char* home)
{
    //printf("开始调用screencap");
    //system(ADB( devices));
    //system("./installold.sh");//没有弹出终端
    char adb_root[500] = {}, desktop[200] = {};
    strcat(desktop, home);
    strcat(desktop, "/Desktop/");
    strcat(adb_root, home);
    strcat(adb_root, "/Library/Android/sdk/platform-tools/adb ");
    char str[500] = {}, str1[500] = {}, str2[500] = {};
    strcat(str, adb_root);
    strcat(str, " shell screencap -p /sdcard/screen.png");
    mysystem(str);
    
    strcat(str1, adb_root);
    strcat(str1, " pull /sdcard/screen.png ");
    strcat(str1, desktop);
    strcat(str1, "/screen.png");
    mysystem(str1);
    
    strcat(str2, "open ");
    strcat(str2, desktop);
    strcat(str2, "/screen.png");
    mysystem(str2);
    
    //    system(ADB( shell screencap -p /sdcard/screen.png));
//    system(ADB( pull /sdcard/screen.png ));
//    system("open screen.png");
}

void _pulltraces(const char* home) {
    //printf("pulltraces");
    char str[500] = {};
    char adb_root[500] = {};
    strcat(adb_root, home);
    strcat(adb_root, "/Library/Android/sdk/platform-tools/adb ");
    strcat(str, adb_root);
    strcat(str, " pull /data/anr ");
    
    char desktop[200] = {};
    strcat(desktop, home);
    strcat(desktop, "/Desktop/");
    strcat(str, desktop);
    mysystem(str);
//    system(ADB( pull /data/anr));
}

void _pullerrors(const char* home, const char* packageName) {
   // printf("pullerrors");
    char str1[500]={},str2[500]={},str3[500]={},str4[500]={},str5[500]={};
    char adb_root[500] = {};
    strcat(adb_root, home);
    strcat(adb_root, "/Library/Android/sdk/platform-tools/adb ");
    strcat(str1, adb_root);
    strcat(str1, " shell date > ");
    char desktop[200] = {};
    strcat(desktop, home);
    strcat(desktop, "/Desktop/");
    strcat(str1, desktop);
    strcat(str1, "_time.txt");
    mysystem(str1);
    strcat(str2, adb_root);
    strcat(str2, " pull /data/anr ");
    strcat(str2, desktop);
    mysystem(str2);
    strcat(str3, adb_root);
    strcat(str3, " shell dumpsys meminfo ");
    strcat(str3, packageName);
    strcat(str3, " > ");
    strcat(str3, desktop);
    strcat(str3, "appmeminfo.txt");
    mysystem(str3);
    
    strcat(str4, adb_root);
    strcat(str4, " shell cat /proc/meminfo > ");
    strcat(str4, desktop);
    strcat(str4, "allmeminfo.txt");
    mysystem(str4);
    
    strcat(str5, adb_root);
    strcat(str5, " logcat -t 2000 > ");
    strcat(str5, desktop);
    strcat(str5, "log.txt");
    mysystem(str5);
//    mysystem(ADB( shell date > _time.txt));
//    system(ADB( pull /data/anr));
//    system(ADB( shell dumpsys meminfo com.qihoo.browser > appmeminfo.txt));
//    system(ADB( shell cat /proc/meminfo > allmeminfo.txt));
//    system(ADB( logcat -t 1500 > log.txt));
}

void _quickRecord(const char* home, const char* _time)
{
    char str[500] = {},str1[500]={},str3[500]={};
    char adb_root[500] = {};
    strcat(adb_root, home);
    strcat(adb_root, "/Library/Android/sdk/platform-tools/adb ");
    strcat(str, adb_root);
    strcat(str, " shell screenrecord --time-limit ");
    strcat(str, _time);
    strcat(str, " /data/local/tmp/record.mp4");
    mysystem(str);
    strcat(str1, adb_root);
    strcat(str1, " pull /data/local/tmp/record.mp4 ");
    char desktop[200] = {};
    strcat(desktop, home);
    strcat(desktop, "/Desktop/");
    strcat(str1, desktop);
    mysystem(str1);
    strcat(str3, "open ");
    strcat(str3, desktop);
    strcat(str3, "record.mp4");
    mysystem(str3);
}

void _install(const char* home, const char* packageName)
{
    char str[500] = {}, str1[500]={};
    char adb_root[500] = {};
    strcat(adb_root, home);
    strcat(adb_root, "/Library/Android/sdk/platform-tools/adb ");
    strcat(str, adb_root);
    strcat(str, "install -r ");
    strcat(str, packageName);
    mysystem(str);
    strcat(str1, adb_root);
    strcat(str1, " shell am start com.p1.mobile.putong/.ui.splash.SplashAct");
    mysystem(str1);
}

void _uninstall(const char* home, const char* packageName)
{
    // printf("%s", packageName);
    char adb_root[500] = {};
    strcat(adb_root, home);
    strcat(adb_root, "/Library/Android/sdk/platform-tools/adb ");
    char str[500]={};
    strcat(str, adb_root);
    strcat(str, " uninstall ");
    strcat(str, packageName);
    mysystem(str);
}

void _killprocess(const char* home, const char* packageName)
{
    char str[500] = {};
    char adb_root[500] = {};
    strcat(adb_root, home);
    strcat(adb_root, "/Library/Android/sdk/platform-tools/adb ");
    strcat(str, adb_root);
    strcat(str, " shell am force-stop ");
    strcat(str, packageName);
    mysystem(str);
}

void _clearAppData(const char* home, const char* packageName)
{
    char str[500] = {};
    char adb_root[500] = {};
    strcat(adb_root, home);
    strcat(adb_root, "/Library/Android/sdk/platform-tools/adb ");
    strcat(str, adb_root);
    strcat(str, " shell pm clear ");
    strcat(str, packageName);
    mysystem(str);
}

void _getAppMemeory(const char* home, const char* packageName)
{
    char str[500] ={}, str1[500];
    char adb_root[500] = {};
    strcat(adb_root, home);
    strcat(adb_root, "/Library/Android/sdk/platform-tools/adb ");
    strcat(str, adb_root);
    strcat(str, " shell dumpsys meminfo ");
    strcat(str, packageName);
    strcat(str, " > ");
    char desktop[200] = {};
    strcat(desktop, home);
    strcat(desktop, "/Desktop/");
    strcat(str, desktop);
    strcat(str, "appmeminfo.txt");
    mysystem(str);
    strcat(str1, "open ");
    strcat(str1, desktop);
    strcat(str1, "appmeminfo.txt");
    mysystem(str1);
    
//    const int MAXPATH=250;
//    char buffer[MAXPATH];
//    getcwd(buffer, MAXPATH);
//    printf("The current directory is: %s", buffer);
}

//https://blog.csdn.net/zjnuwsf/article/details/52669764
// adb shell monkey -p com.xy.android.junit -s 500 --ignore-crashes --ignore-timeouts --monitor-native-crashes -v -v 10000 > E:\monkey_log\java_monkey_log.txt  //monkey测试
void _monkeyTest(const char* home, const char*packageName)
{
    char str[600] = {};
    char adb_root[500] = {};
    strcat(adb_root, home);
    strcat(adb_root, "/Library/Android/sdk/platform-tools/adb ");
    strcat(str, adb_root);
    strcat(str, "shell monkey -p ");
    strcat(str, packageName);
    strcat(str, " -s 500 --ignore-crashes --ignore-timeouts --monitor-native-crashes -v 10000 > ");
    
    char desktop[200] = {};
    strcat(desktop, home);
    strcat(desktop, "/Desktop/");
    strcat(str, desktop);
    strcat(str, "monkey.log");
    mysystem(str);
}

// 废弃 error解析
void _resolveError_(const char* filePath)
{
    char str[500] = {}, folderPath[500] = {}, tmp[500]={},fileName[200]={};
    int lastSplitIndex = 0;
    for (int i=0;i<strlen(filePath);i++) {
        if (filePath[i] == '/') {
            lastSplitIndex = i;
        }
    }
    strncat(folderPath, filePath, lastSplitIndex);
    folderPath[lastSplitIndex+1] = '\0';
//    printf("%s",folderPath);
    strcat(tmp, "cd ");
    strcat(tmp, folderPath);
//    mysystem(tmp);
    int nameLength = 0;
    for (int i=lastSplitIndex+1; i<strlen(filePath); i++) {
        fileName[i-lastSplitIndex-1] = filePath[i];
        nameLength++;
    }
    fileName[nameLength] = '\0';
   // printf("\nfileName length:%d fileName:%s\n", nameLength,fileName);
    strcat(str, "java ");
    strcat(str, fileName);
//    mysystem(str);
    
    mysystem("java -jar retrace.jar -verbose tantan-3.2.8_233.txt 3.2.8crash.txt > jixie1.txt");
}

void _resolveError(const char* mappingTxt, const char* errorTxt)
{
    char str[900] = {};
    strcat(str, "java -jar retrace.jar -verbose ");
    strcat(str, mappingTxt);
    strcat(str, " ");strcat(str, errorTxt);
    strcat(str, " > /Users/zhangqianchu/resolveError.txt");
    mysystem(str);
}

//打印当前路径：
//char   buffer[200];
//getcwd(buffer, 200);
//printf( "The   current   directory   is:   %s ",   buffer);

void _copyToPc(const char* adb_root, const char* source, const char* des)
{
    char str[380] = {};
    strcat(str, adb_root);
    strcat(str, "pull /sdcard/");
    strcat(str, source);
    strcat(str, " ");
    strcat(str, des);
    mysystem(str);
}

void _copyToPhone(const char* adb_root, const char* source, const char* des)
{
    char str[400] = {};
    strcat(str, adb_root);
    strcat(str, "push ");
    strcat(str, source);
    strcat(str, " /sdcard/");
    strcat(str, des);
    mysystem(str);
}
